package com.sx.jys.ui.frag

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.gyf.immersionbar.ImmersionBar
import com.lxj.xpopup.XPopup
import com.sanxinsaas.nearstore.framework.util.click
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sos.widget.view.refresh.finishLoad
import com.sx.jys.R
import com.sx.jys.app.framework.BaseFragment
import com.sx.jys.databinding.FragAccountBinding
import com.sx.jys.ui.adapter.ItemAccountAdapter
import com.sx.jys.ui.vm.AccountVm

class FragAccount : BaseFragment<AccountVm, FragAccountBinding>() {
    companion object {
        val instance = FragAccount()
    }

    override fun initView(savedInstanceState: Bundle?) {
        (mDatabind.layoutTitle.layoutParams as ViewGroup.MarginLayoutParams).let {
            it.topMargin = ImmersionBar.getStatusBarHeight(this)
        }
        mDatabind.myRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                refreshLayout.finishRefresh(2500)
                mViewModel.reqAccountList()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                refreshLayout.finishLoadMore(2500)
                mViewModel.reqAccountList()
            }
        })

        mDatabind.imageGg.click {
            OptionsPickerBuilder(requireContext(), object : OnOptionsSelectListener {
                override fun onOptionsSelect(
                    options1: Int,
                    options2: Int,
                    options3: Int,
                    v: View?
                ) {
                    val value=options1+1
                    mViewModel.leverageAllLimit(value.toString())
                }

            }).setTitleText("请选择杠杆倍数")
                .setLabels("倍", "", "")
                .build<String>()
                .apply {
                    setPicker((1 until 101).toList().map { it.toString() })
                }
                .show()
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.vm = mViewModel
        mDatabind.adapter = ItemAccountAdapter(requireContext()).also {
            it.setOnItemClickListener { viewId, item, position ->
                if (viewId == R.id.reset_value) {
                    XPopup.Builder(requireContext())
                        .asInputConfirm("保证金限制", null,item.marginLimit, "请输入保证金限制金额") {
                            mViewModel.resetValue(item.id, it)
                        }.show()
                }
            }
        }
        mViewModel.reqAccountList()
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.uiState.refreshEvent.observe(this) {
            mDatabind.myRefresh.finishRefresh()
        }
        mViewModel.uiState.loadMoreEvent.observe(this) {
            mDatabind.myRefresh.finishLoad(it)
        }
    }
}